<%@ page import="db.DriversDao" %>
<%@ page import="beans.Drivers" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>公司用车管理系统（司机队长）- 司机信息</title>
    <style>
        /* 全局样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
            color: #333;
        }

        /* 头部样式 */
        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }
        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
        }
        .user-info {
            margin-right: 20px;
            color: #fff;
        }
        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
        }
        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
        }

        /* 主体容器 */
        .container {
            display: flex;
            height: calc(100vh - 56px);
        }

        /* 侧边栏样式 */
        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24,58,138,0.05);
        }
        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: background 0.2s, border 0.2s;
            text-decoration: none;
            display: block;
        }
        .menu-item.active, .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24,58,138,0.10);
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            background: #fff;
            border: 2px solid #3ba0e9;
            border-left: none;
            padding: 30px;
            overflow-y: auto;
        }
        .page-title {
            font-size: 2.2rem;
            color: #183a8a;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }
        .action-bar {
            display: flex;
            gap: 20px;
            margin-bottom: 25px;
        }
        .action-btn {
            padding: 8px 20px;
            background: #1da1e7;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: background 0.2s;
            text-decoration: none;
            display: inline-block;
        }
        .action-btn:hover {
            background: #2176b8;
        }
        .divider {
            height: 1px;
            background: #ddd;
            margin: 20px 0;
        }

        /* 搜索框样式 */
        .search-bar {
            display: flex;
            gap: 15px;
            margin-bottom: 25px;
            align-items: center;
            flex-wrap: wrap;
        }
        .search-field {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }
        .search-btn {
            padding: 8px 20px;
            background: #1da1e7;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1.1rem;
            cursor: pointer;
        }
        .search-btn:hover {
            background: #2176b8;
        }

        /* 表格样式 */
        .driver-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .driver-table th {
            background: #f0f4f8;
            padding: 12px 15px;
            text-align: left;
            font-weight: bold;
            border-bottom: 2px solid #ddd;
        }
        .driver-table td {
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
        }
        .driver-table tr:hover {
            background-color: #f5f9ff;
        }

        /* 状态标签样式 */
        .status-badge {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.9rem;
        }
        .status-空闲 {
            background: #d4edda;
            color: #155724;
        }
        .status-使用 {
            background: #fff3cd;
            color: #856404;
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .sidebar {
                width: 120px;
            }
            .menu-item {
                padding-left: 15px;
                font-size: 1rem;
            }
            .action-bar {
                flex-direction: column;
                gap: 10px;
            }
            .search-bar {
                flex-direction: column;
                align-items: stretch;
            }
            .search-field {
                width: 100%;
            }
            .driver-table {
                display: block;
                overflow-x: auto;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div>欢迎登录公司用车管理系统（司机队长）</div>
        <div class="header-right">
            <div class="user-icon">👤</div>
            <div class="user-info">账号：<%=session.getAttribute("username")%></div>
            <div class="bell-icon">🔔</div>
            <a href="${pageContext.request.contextPath}/LogoutServlet" class="logout">退出登录</a>
        </div>
    </div>

    <div class="container">
        <div class="sidebar">
            <a class="menu-item" href="DriversServlet?action=all3">系统首页</a>
            <a class="menu-item" href="DriversServlet?action=all2">个人中心</a>
            <a class="menu-item" href="ShenqingServlet?action=all">我的待办</a>
            <a class="menu-item" href="UseOrderServlet?action=list">我处理的</a>
            <a class="menu-item" href="VehiclesServlet?action=all">全部车辆信息</a>
            <a class="menu-item active" href="DriversServlet?action=all">全部司机信息</a>
        </div>

        <div class="main-content">
            <h1 class="page-title">司机信息管理</h1>

            <div class="action-bar">
                <a href="DriversServlet?action=add" class="action-btn">增加司机</a>
            </div>

            <% if(request.getAttribute("message") != null) { %>
                <div class="alert alert-success"><%=request.getAttribute("message")%></div>
            <% } %>
            <% if(request.getAttribute("error") != null) { %>
                <div class="alert alert-danger"><%=request.getAttribute("error")%></div>
            <% } %>

            <!-- 搜索表单 -->
            <form action="DriversServlet" method="get" class="search-bar">
                <input type="hidden" name="action" value="search">
                <input type="text" name="driverId" placeholder="司机ID" class="search-field" value="${param.driverId}">
                <input type="text" name="driverName" placeholder="司机姓名" class="search-field" value="${param.driverName}">
                <input type="text" name="phoneNumber" placeholder="联系电话" class="search-field" value="${param.phoneNumber}">
                <select name="gender" class="search-field">
                    <option value="">性别</option>
                    <option value="男" ${param.gender == '男' ? 'selected' : ''}>男</option>
                    <option value="女" ${param.gender == '女' ? 'selected' : ''}>女</option>
                </select>
                <select name="status" class="search-field">
                    <option value="">全部状态</option>
                    <option value="空闲" ${param.status == '空闲' ? 'selected' : ''}>空闲</option>
                    <option value="使用" ${param.status == '在职' ? 'selected' : ''}>使用</option>
                </select>
                <button type="submit" class="search-btn">搜索</button>
                <a href="DriversServlet?action=all" class="action-btn">重置</a>
            </form>

            <div class="divider"></div>

            <table class="driver-table">
                <thead>
                    <tr>
                        <th>司机ID</th>
                        <th>姓名</th>
                        <th>联系电话</th>
                        <th>电子邮箱</th>
                        <th>性别</th>
                        <th>状态</th>
                        <th>驾驶证号码</th>
                        <th>驾驶证有效期</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% 
                    List<Drivers> driverList = (List<Drivers>)request.getAttribute("driverList");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    if(driverList != null && !driverList.isEmpty()) {
                        for(Drivers driver : driverList) { 
                    %>
                    <td><%= driver.getDriverId() %></td>
                    <td><%= driver.getDriverName() %></td>
                    <td><%= driver.getPhoneNumber() %></td>
                    <td><%= driver.getEmail() != null ? driver.getEmail() : "" %></td>
                    <td><%= driver.getGender() != null ? driver.getGender() : "" %></td>
                    <td><%= driver.getStatus() != null ? driver.getStatus() : "" %></td>
                    <td><%= driver.getDriverLicenseNumber() != null ? driver.getDriverLicenseNumber() : "" %></td>
                    <td><%= driver.getDriverLicenseExpirationDate() != null ? sdf.format(driver.getDriverLicenseExpirationDate()) : "N/A" %></td>
                            <td>
                                <a href="DriversServlet?action=edit&driverId=<%= driver.getDriverId() %>" class="action-btn">编辑</a>

                                <a href="DriversServlet?action=delete&driverId=<%= driver.getDriverId() %>&name=<%= driver.getDriverName() %>"
                                   onclick="return confirm('确定删除司机 <%= driver.getDriverName() %> 吗？') " class="action-btn">删除</a>
                            </td>
                        </tr>
                    <% } } else { %>
                        <tr>
                            <td colspan="9" style="text-align: center;">暂无司机信息</td>
                        </tr>
                    <% } %>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        // 删除司机
        function deleteDriver(id) {
            if(confirm('确定要删除这个司机吗？此操作不可恢复。')) {
                window.location.href = '${pageContext.request.contextPath}/DriversServlet?action=delete&id=' + id;
            }
        }
    </script>
</body>
</html>